package client.comm.commlib.widget

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.LayoutInflater
import android.widget.CheckedTextView
import android.widget.FrameLayout
import androidx.appcompat.widget.LinearLayoutCompat
import androidx.databinding.DataBindingUtil
import androidx.databinding.ObservableField
import client.comm.commlib.R
import client.comm.commlib.comm_ui.bean.SkuObj
import client.comm.commlib.comm_ui.bean.SkuProps
import client.comm.commlib.databinding.LayoutSkuitemBinding
import client.comm.commlib.databinding.LayoutSkutxtBinding

class SkuTypeLayout @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null
) : LinearLayoutCompat(context, attrs) {


    init {
        orientation = VERTICAL
    }

    lateinit var styleAttrs: List<SkuProps>
    lateinit var skuList: List<SkuObj>

    var selectSkuChange: ((SkuObj?) -> Unit)? = null

    fun setData(d: List<SkuObj>) {
        skuList = d
        val datas: List<SkuProps> = switchSku(d)
        styleAttrs = datas
        for ((pindex, s) in datas.withIndex()) {
            val skuitemBinding = LayoutSkuitemBinding.inflate(
                LayoutInflater.from(context),
                this, false
            )
            s.selectIndex.set(0)
            skuitemBinding.bean = s
            for ((i, ss) in s.attrs.withIndex()) {
                skuitemBinding.skuWrap.addView(
                    DataBindingUtil.inflate<LayoutSkutxtBinding>(
                        LayoutInflater.from(context),
                        R.layout.layout_skutxt,
                        skuitemBinding.skuWrap,
                        false
                    ).apply {
                        ss.isSelect.set(i == s.selectIndex.get())
                        bean = ss
                        text.setOnClickListener {
                            val oIdx = s.selectIndex.get() as Int
                            if (oIdx >= 0 && s.attrs.get(oIdx).isSelect.get() == true) {
                                s.attrs.get(oIdx).isSelect.set(false)
                            }
                            if (i == s.selectIndex.get()) {
                                s.selectIndex.set(-1)
                            } else
                                s.selectIndex.set(i)
                            ss.isSelect.set(i == s.selectIndex.get())

                            if (styleAttrs.size > 1) {
                                updateSkuDisable(pindex)
                            }
                            updateSelectSku()
                        }
                    }.root
                )
            }
            addView(skuitemBinding.root)
        }
        updateSelectSku()

    }

    fun updateSkuDisable(parentindex: Int) {
        var selectedCount = 0
        var emptyIndex = 0
        var ids = arrayListOf<String>()

        for ((i, tmp) in styleAttrs.withIndex()) {
//            if (i != parentindex) {
            for (t in tmp.attrs) {
                t.isEnable.set(true)
            }
//            }
            if (tmp.selectIndex.get() as Int >= 0) {
                selectedCount++
                ids.add(tmp.name + ":" + tmp.attrs[tmp.selectIndex.get() as Int].name)
            } else {
                emptyIndex = i
                ids.add("11")
            }
        }
        if (selectedCount == styleAttrs.size) {
            if (parentindex == 0) {
                emptyIndex = styleAttrs.size - 1
            } else {
                emptyIndex = parentindex - 1
            }
            for (tmpObj in styleAttrs[emptyIndex].attrs) {
                ids[emptyIndex] = styleAttrs[emptyIndex].name + ":" + tmpObj.name

                val strId = ids.joinToString(separator = ";")
                val goods = skuList.filter { item -> item.goodsSpec.equals(strId) }

                if (goods.size > 0) {
//                    if (goods[0].status == 0) {
//                        tmpObj.isEnable.set(false)
//                    }
                } else {
                    tmpObj.isEnable.set(false)
                }
            }
        } else if (selectedCount == styleAttrs.size - 1) {

            for (tmpObj in styleAttrs[emptyIndex].attrs) {
                ids[emptyIndex] = styleAttrs[emptyIndex].name + ":" + tmpObj.name

                val strId = ids.joinToString(separator = ";")
                val goods = skuList.filter { item -> item.goodsSpec.equals(strId) }

                if (goods.size > 0) {
//                    if (goods[0].status == 0) {
//                        tmpObj.isEnable.set(false)
//                    }
                } else {
                    tmpObj.isEnable.set(false)
                }
            }

        }
    }

    private fun updateSelectSku() {
        val attrs = styleAttrs.map { item ->
            if (item.selectIndex.get() as Int >= 0) item.name + ':' + item
                .attrs[item.selectIndex.get() as Int].name else item.name + ':' + ""
        }.joinToString(";")

        val selectSku = skuList.filter { item -> item.goodsSpec.equals(attrs) }
        selectSkuChange?.invoke(if (selectSku.size > 0) selectSku[0] else null)
    }

    // {
    //     "name": "",
    //     "attrs": [{name:""}]}]
    // }
    fun switchSku(datas: List<SkuObj>): List<SkuProps> {

        val style = ArrayList<SkuProps>()
        for (e in datas) {
            val spec = e.goodsSpec.split(";")
            if (spec.size > 0) {
                for (ee in spec) {
                    val s = ee.split(":")
                    val styleName = s[0]
                    val styleAttr = s[1]
                    var hasName = false

                    for (ss in style) {

                        if (ss.name.equals(styleName)) {
                            hasName = true
                            val a = ss.attrs.filter { item -> item.name.equals(styleAttr) }

                            if (a.size == 0) {
                                ss.attrs as MutableList
                                ss.attrs.add(SkuProps.Sku(name = styleAttr))
                            }
                        }
                    }

                    if (!hasName) {
                        style.add(SkuProps(name = styleName, ArrayList<SkuProps.Sku>().apply {
                            add(SkuProps.Sku(name = styleAttr))
                        }))
                    }


                }
            }

        }

        return style
    }

    fun getRetDatas(): String {

        return "bean"
    }

}